package com.vip.jwt.controller;

import cn.hutool.core.util.RandomUtil;
import com.alipay.api.AlipayApiException;
import com.vip.jwt.common.entity.R;
import com.vip.jwt.common.utils.*;
import com.vip.jwt.common.utils.app.APPWXAuthUtil;
import com.vip.jwt.common.utils.app.APPWXPayUtil;
import com.vip.jwt.common.utils.app.APPZFBPayUtil;
import com.vip.jwt.common.utils.sign.WXSignUtil;
import com.vip.jwt.common.utils.sign.ZFBSignUtil;
import com.vip.jwt.common.utils.xcx.wx.*;
import com.wf.captcha.SpecCaptcha;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LoginController extends BaseController{

    /**
     * 验证码
     * @return
     */
    @GetMapping("/images/captcha")
    public Object captcha(){
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String verCode = specCaptcha.text();
        String verKey = RandomUtil.randomString(10);
        RedisUtil.set(verKey,verCode);
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("ver_key",verKey);
        map.put("ver_code",specCaptcha.toBase64());
        return R.ok().data(map);
    }

    @PostMapping("/login")
    public Object login(HttpServletRequest request){
        //verKey和verCode是前台传过来的，这里为了测试，没有体现出来
        String verKey = "";
        String verCode = "";
        String findVerCode = RedisUtil.get(verKey);
        String username = "123";
        String password = "456";
        String salt = "salt";
        //这里存入Redis中的password是加密之后的
        String token = JwtUtil.sign(username,password);
        JwtUtil.set(request,username,token);
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("id",1);
        map.put("username",username);
        map.put("token",token);
        return R.ok().data(map);
    }

    @PostMapping("/xcx/auth")
    public Object auth(@RequestParam String code, @RequestParam String openid){
//        WXAPPUtil.getAccessToken(code);
        APPWXAuthUtil.getWXUserInfo(code,openid);
//        Map<String, String> sessionKeyAndOpenid = WXXCXUtil.getSessionKeyAndOpenid(code);
//        if(null != sessionKeyAndOpenid){
//            String openid = sessionKeyAndOpenid.get("openid");
//            System.out.println(openid);
//        }
        return R.error();
    }

    @PostMapping("/xcx/phone/{code}")
    public Object auth(@RequestBody WXEncyPhone wxEncyPhone,@PathVariable String code){
        return null;
    }

    @GetMapping("/test/{code}/{accessToken}")
//    @RequiresPermissions("123")
    public Object test(@PathVariable String code,@PathVariable String accessToken){
//        String s = APPWXPayUtil.refund("20200925103855816664091882",WXSignUtil.getOutRefundNo(),"100","100");
//        System.out.println(s);
//        WXUserInfo wxUserInfo = APPWXAuthUtil.getWXUserInfo(accessToken, openId);
//        String test = APPWXPayUtil.transferAccounts(WXSignUtil.getOutTradeNo(), wxUserInfo.getOpenid(), "100", "测试");
//        System.out.println(test);
//        WXUserInfo wxUserInfo = APPWXAuthUtil.getWXUserInfo(accessToken, openId);
//        System.out.println(wxUserInfo);
//        WXSo wxSo = WXAuthUtil.getSessionKeyAndOpenid(openId);
//        String openIds = wxSo.getOpenid();
//        String test = APPWXPayUtil.transferAccounts(WXSignUtil.getOutTradeNo(), openIds, "100", "测试");
//        System.out.println(test);
//        try {
//            String s = APPZFBPayUtil.transferAccounts("测试", ZFBSignUtil.getOutTradeNo(), "1", "17560360213", "张贵宾");
//            System.out.println(s);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
        return R.ok();
    }

}

